<?php
/* @var $this TrxReceiptController */
/* @var $model TrxReceipt */
/* @var $form TbActiveForm */
?>

<div class="form">

    <?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
	'id'=>'trx-receipt-form',
	// Please note: When you enable ajax validation, make sure the corresponding
	// controller action is handling ajax validation correctly.
	// There is a call to performAjaxValidation() commented in generated controller code.
	// See class documentation of CActiveForm for details on this.
	'enableAjaxValidation'=>false,
)); ?>

    <p class="help-block">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

            <div class="row">
	<div class="col-md-12">
						<div class="widget box">
							<div class="widget-header">
								<div class="col-md-6">
								<h4><i class="icon-reorder"></i>Receipt From : </h4>
								<div class="controls">
				<div class="input-group">
				 <?php echo $form->textField($model,'client_name',array('class'=>'form-control input-width-medium','id'=>'client_name_1','maxlength'=>50,'style'=>'float:left')); ?>
				 <?php echo $form->hiddenField($model, 'mst_client_id',array('class'=>'form-control input-width-small','id'=>'mst_client_id_1','readonly'=>true)); ?>
					<a class="btn btn-default" href="#myModal" data-toggle="modal" style="float:left">+</a>
				</div>
								</div>
								<h4><i class="icon-reorder"></i>Bank </h4>
								<div class="controls">
				<div class="input-group">
				 <?php echo $form->dropDownList($model,'coa_debet_id',CHtml::listData(MstCoa::model()->findAll('id >= 11 and id <= 16 '), 'id','name'), array('class'=>'form-control')); ?>
				</div>
								</div>
								<h4><i class="icon-reorder"></i>Tgl. Transaksi </h4>
								<div class="controls">
				<div class="input-group">
				 <?php echo $form->textField($model,'tgl_transaksi', array('class'=>'form-control input-width-small','data-mask'=>"99-99-9999",'value'=>date('d-m-Y'))); ?>
				</div>
								</div>
								<div class="toolbar no-padding">
									<div class="btn-group">
										<span class="btn btn-xs widget-collapse"><i class="icon-angle-down"></i></span>
									</div>
								</div>
						</div>
					
						<div class="col-md-6">
								
								<h4><i class="icon-reorder"></i>Total Rp : </h4>
								<div class="controls">
									<label id="lbl_totalrp">Rp 0</label>
								</div>
								<h4><i class="icon-reorder"></i>Total $ : </h4>
								<div class="controls">
									<label id="lbl_totaldolar">$ 0</label>
								</div>
								<h4><i class="icon-reorder"></i>Total EURO : </h4>
								<div class="controls">
									<label id="lbl_totaleur">EUR 0</label>
								</div>
								<input type="button" value="Total" class="btn btn-primary" id="btn_total">
								
						</div>
							</div>
							<div class="widget-content no-padding">
							<input type="text" id="flag_notanote" />
<table class="table table-hover table-striped table-bordered table-highlight-head">
	<thead>
		<tr>
			<th>No</th>
			<th>Jenis Trx</th>
			<th>DN/CN <br> <font size="1pt" color="red"></th>
			<th>Tagihan</th>
			<th>COA Kredit</th>
			<th>Jenis Mata Uang</th>
			<th>Nominal</th>
			<th>Keterangan</th>
		</tr>
	</thead>
	<tbody>
		<?php 
	$a = 10;
	for($i=0;$a > $i; $i++){
	$nume = $i+1;
	?>
		<tr>
			<td><?php echo $nume; ?></td>
			<td>
			<?php echo $form->dropDownList($model, 'jns_trx', array(0 => "Pilih",1 => "Premi", 2 => "Non Premi", 3 => "Lain-lain"),array('class'=>'form-control input-width-small','id'=>'jns_trx_'.$nume,'onChange'=>'enableNota('.$nume.')','name'=>'jns_trx[]')); ?>
			</td>
			<td>
				<div class="input-group">
				 <?php echo $form->textField($model,'nota_note',array('class'=>'form-control input-width-medium','id'=>'nota_note_'.$nume,'maxlength'=>50,'style'=>'float:left','readonly'=>true,'onkeydown'=>"showData(event,".$nume.")",'name'=>'nota_note[]')); ?>
				 <a class="btn btn-default" href="#modalNota" data-toggle="modal" style="float:left" onClick="flagnotanote(<?php echo $nume; ?>)">+</a>
				 <?php echo $form->hiddenField($model, 'no_nota_id',array('class'=>'form-control input-width-small','id'=>'no_nota_id_'.$nume,'readonly'=>true)); ?>
				<input type="hidden" name="status_hg_nh[]" id="status_hg_nh_<?php echo $nume; ?>" />
				<input type="hidden" name="coa_credit_id[]" id="coa_credit_id_<?php echo $nume; ?>" />
				</div>
			</td>
			
			<td><label id="lbl_tagihan_<?php echo $nume; ?>"></label></td>
			<td>
				<?php 
//echo CHtml::dropDownList('coa_kredit', '', CHtml::listData(MstCoa::model()->findAll(), 'id','name'),array("empty"=>"--Pilih COA Kredit--","id"=>'coa_kredit_'.$nume,"class"=>"form-control",'onChange'=>'getKredit('.$nume.')'));							?>
<select  id="coa_kredit_<?=$nume?>" class="select2-select-00 col-md-12 full-width-fix" onChange="getKredit('<?=$nume?>')">
							<option></option>
							<?php 
							foreach($data as $datas):
							?>
							<option value="<?=$datas['id'];?>"><?=$datas['name']." (".$datas['code'].")";?></option>
							<?php endforeach; ?>
						</select>
			</td>
			<td><?php echo $form->dropDownList($model, 'jns_mata_uang', array(1 => "Rupiah", 2 => "$", 3 => "Euro"),array('prompt'=>'Pilih Mata Uang','class'=>'form-control input-width-small','id'=>'jns_mata_uang_'.$nume,'name'=>'jns_mata_uang[]', 'onchange'=>'gantimata('.$nume.');')); ?></td>
			<td>
				<?php echo $form->textField($model,'nominal', array('class'=>'form-control input-width-small nomi', 'data-uang'=>$datatransaksi[$i]['jns_mata_uang'],'name'=>'nominal[]', 'id'=>'nomi_'.$nume)); ?>
			</td>
			<td>
				<?php echo $form->textArea($model,'keterangan', array('class'=>'form-control input-width-small','name'=>'keterangan[]')); ?>
			</td>
		</tr>
		<?php } ?>
		
	</tbody>
</table>


							</div>
						</div>
					</div>
	</div>

        <div class="form-actions">
        <?php echo TbHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array(
		    'color'=>TbHtml::BUTTON_COLOR_PRIMARY,
		    'size'=>TbHtml::BUTTON_SIZE_LARGE,
		)); ?>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->


<div id="myModal" class="modal fade in" style="display: none;" aria-hidden="false">
												<div class="modal-dialog" style="width:700px;">
													<div class="modal-content">
														<div class="modal-header">
															<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
															<h4 class="modal-title">Pilih Client</h4>
														</div>
														<div class="modal-body">
															
							<?php echo $this->renderPartial('_viewAllclient', array('model'=>$model3)); ?> 
														</div>
														<div class="modal-footer">
															<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
															<button id="btnTest" data-dismiss="modal" class="btn btn-primary" type="button">Pilih</button>
														</div>
													</div><!-- /.modal-content -->
												</div><!-- /.modal-dialog -->
</div>

<div id="modalNota" class="modal fade in" style="display: none;" aria-hidden="false">
												<div class="modal-dialog" style="width:700px;">
													<div class="modal-content">
														<div class="modal-header">
															<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
															<h4 class="modal-title">Pilih Nomor Nota</h4>
														</div>
														<div class="modal-body">
															 <font color="red"> NK : Nota Kredit || P : Premi || GK : Ganti Kartu </font>
															<table id="tblListnota" class="table table-striped table-bordered table-hover table-checkable datatable dataTable" style="width:600px;">
									<thead>
										<tr>
											<th>Jenis</th>
											<th>Nomor Nota</th>
											<th>Nominal</th>
											<th>Pilih</th>
										</tr>
									</thead>
									<tbody>
										
									</tbody>
								</table>
														</div>
														<div class="modal-footer">
															<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
															<button id="btnPilihnota" data-dismiss="modal" class="btn btn-primary" type="button">Pilih</button>
														</div>
													</div><!-- /.modal-content -->
												</div><!-- /.modal-dialog -->
</div>

<div id="myModal2" class="modal fade in" style="display: none;" aria-hidden="false">
												<div class="modal-dialog" style="width:700px;">
													<div class="modal-content">
														<div class="modal-header">
															<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
															<h4 class="modal-title">Pilih Client</h4>
														</div>
														<div class="modal-body">
													<input type="hidden" id="txt_get_id" />		
							<?php echo $this->renderPartial('_viewAllnota', array('model'=>$model2)); ?> 
														</div>
														<div class="modal-footer">
															<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
															<button id="btnTest2" data-dismiss="modal" class="btn btn-primary" type="button">Pilih</button>
														</div>
													</div><!-- /.modal-content -->
												</div><!-- /.modal-dialog -->
</div>
	
<script type="text/javascript">
function formatCurrency(total) {
    var neg = false;
    if(total < 0) {
        neg = true;
        total = Math.abs(total);
    }
    return (neg ? "" : '') + parseFloat(total, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString();
}
function gantimata(id){
	$('#nomi_'+id).attr('data-uang',$('#jns_mata_uang_'+id).val());
}
/*
$('.nomi').keyup(function(){
	var sumrp = 0;
	var sumdolar = 0;
	var sumeur = 0;
	$('.nomi').each(function(){
		if($(this).attr('data-uang') == '1'){
		sumrp += parseFloat(this.value);
		}else if($(this).attr('data-uang') == '2'){
		sumdolar += parseFloat(this.value);
		}else if($(this).attr('data-uang') == '3'){
		sumeur += parseFloat(this.value);
		}else{
		
		}
		$('#lbl_totalrp').html(formatCurrency(sumrp));
		$('#lbl_totaldolar').html(sumdolar);
		$('#lbl_totaleur').html(sumeur);
	});
});*/
function flagnotanote(id){
	$("#flag_notanote").val(id);
}
function showData(e,id){
	if($("#jns_trx_"+id).val()=="1" || $("#jns_trx_"+id).val()=="2"){
	var code = e.keyCode || e.which;
	  if (code == 9){
		//alert($("#nota_note_"+id).val());
			$.ajax({
			type:"POST",
            url:"<?php echo Yii::app()->createUrl('/TrxReceipt/showTagihan'); ?>",
			data:"id="+id+"&no_nota="+$("#nota_note_"+id).val()+"&jns_trx="+$("#jns_trx_"+id).val(),
			dataType:"json",
            success:function(data){
				if(data.id==""){
					alert('No Nota Tidak Sesuai, Cek Kembali');
					$("#nota_note_"+id).focus();
				}else{
					if(data.code=='IDR'){
						var isi = data.code+' '+data.final_nominal;
					}else{
						var isi = data.code+' '+data.nominal+' <br/> Konversi : '+ 'IDR '+data.final_nominal;
					}
					$("#lbl_tagihan_"+id).html(isi);
					$("#no_nota_id_"+id).val(data.id);
					$("#status_hg_nh_"+id).val(data.jns_insurance);
					$("#coa_credit_id_"+id).val(data.id_insurance);
				}
			}
		});
	  }//end if
	}
}

function getKredit(id){
	
	$("#coa_credit_id_"+id).val($("#coa_kredit_"+id).val());
	
}
 
function enableNota(id){
	if($("#jns_trx_"+id).val() != '0'){
		$("#nota_note_"+id).attr("readonly",false);
		$("#nota_note_"+id).focus();
	}else{
		$("#nota_note_"+id).attr("readonly",false);
		$("#nota_note_"+id).focus();
	}
	
}

$("#btnPilihnota").click(function()
{
	var id = $("#flag_notanote").val();
	if($('input:radio[name=rdo_nota]:checked').attr('code')=='IDR'){
						var isi = $('input:radio[name=rdo_nota]:checked').attr('code')+' '+$('input:radio[name=rdo_nota]:checked').attr('final_nominal');
					}else{
						var isi = $('input:radio[name=rdo_nota]:checked').attr('code')+' '+$('input:radio[name=rdo_nota]:checked').attr('nominal')+' <br/> Konversi : '+ 'IDR '+$('input:radio[name=rdo_nota]:checked').attr('final_nominal');
					}
	$("#lbl_tagihan_"+id).html(isi);
	$("#nomi_"+id).val($('input:radio[name=rdo_nota]:checked').attr('final_nominal'));
	$("#no_nota_id_"+id).val($('input:radio[name=rdo_nota]:checked').val());
	$("#status_hg_nh_"+id).val($('input:radio[name=rdo_nota]:checked').attr('jns_insurance'));
	$("#coa_credit_id_"+id).val($('input:radio[name=rdo_nota]:checked').attr('id_insurance'));
	$("#nota_note_"+id).val($('input:radio[name=rdo_nota]:checked').attr('nota_debet_no'));
	$("#jns_mata_uang_"+id).val('1');
});

$("#btnTest").click(function()
{
	var id_n = $("#flag_notanote").val();
	var nil = "";
    nil = $('input:radio[name=user]:checked').val();
   $("#mst_client_id_1").val(nil);
   $("#client_name_1").val($('input:radio[name=user]:checked').attr('nama_client'));
   $.ajax({
			type:"POST",
            url:"<?php echo Yii::app()->createUrl('/trxReceipt/showListnota'); ?>",
			data:{"id_client":nil,"jns_trx":$("#jns_trx_"+id_n).val()},
			dataType:"json",
            success:function(data){
				var isi = "dfds";
				for(var i=0;i<data.length;i++){
					isi += "<tr>" +
						"<td>"+data[i].jns_insurance+"</td>"+
						"<td>"+data[i].nota_debet_no+"</td>"+
						"<td>"+data[i].code+" "+ formatCurrency(data[i].nominal) +"</td>"+
						"<td><input id='rdo_nota' type='radio' name='rdo_nota' nota_debet_no='"+data[i].nota_debet_no+"' nominal='"+data[i].nominal+"'  final_nominal='"+data[i].final_nominal+"' code='"+data[i].code+"' jns_insurance='"+data[i].jns_insurance+"' id_insurance='"+data[i].id_insurance+"' value='"+data[i].id+"'></td>"+
						"</tr>";
						/*$('#tblListnota').dataTable().fnAddData( [
                                                                          isi,
                                                                          isi,
                                                                          isi,
                                                                          isi ]
                                                                        );  */
				}
				
				$("#tblListnota tbody").html(isi);
				var oTable = $("#tblListnota").dataTable();
				oTable.fnDestroy();
				$("#tblListnota").dataTable();
				$("#tblListnota").dataTable().fnSort( [ [1,'asc'] ] );
				
				//$('#tblListnota').dataTable().fnClearTable();
				//$("#tblListnota").dataTable().fnAddData([isi]);
				//$('#tblListnota').dataTable.fnDraw();
				//$('#tblListnota').dataTable.fnClearTable();
				//$('#tblListnota').dataTable.fnDraw();
			}
	});
}); 

$("#btn_total").click(function()
{
	var total = 0.0;
	var total_dolar = 0.0;
	var total_euro = 0.0;
	$('input[name="nominal[]"]').each(function(i,item) {
        var item = parseFloat($(this).val());
        if (isNaN(item)) item = 0;
		
		if( $('#jns_mata_uang_'+parseInt(i+1)).val() == "2"){
			if($("#status_hg_nh_"+parseInt(i+1)).val() == "NK Health" || $("#status_hg_nh_"+parseInt(i+1)).val() == "NK Non Health"){
				total_dolar += item;
			}else{
				total_dolar += item;
			}
		}
		
		if( $('#jns_mata_uang_'+parseInt(i+1)).val() == "1"){
			if($("#status_hg_nh_"+parseInt(i+1)).val() == "NK Health" || $("#status_hg_nh_"+parseInt(i+1)).val() == "NK Non Health"){
				total += item;
			}else{
				total += item;
			}
		}
		if( $('#jns_mata_uang_'+parseInt(i+1)).val() == "3"){
			if($("#status_hg_nh_"+parseInt(i+1)).val() == "NK Health" || $("#status_hg_nh_"+parseInt(i+1)).val() == "NK Non Health"){
				total_euro += item;
			}else{
				total_euro += item;
			}
		}
        
    });
	$('#lbl_totalrp').html("Rp. "+formatCurrency(total));
	$('#lbl_totaldolar').html("$ "+total_dolar);
	$('#lbl_totaleur').html("E "+total_euro);
}); 


/*
function getId(id){
	$("#txt_get_id").val(id);
}

$(document).ready(function(){

$("#btnTest").click(function()
{
	
	var nil = "";
    nil = $('input:radio[name=user]:checked').val();
   $("#mst_client_id_1").val(nil);
   $("#client_name_1").val($('input:radio[name=user]:checked').attr('nama_client'));
}); 

$("#btnTest2").click(function()
{
	var id = $("#txt_get_id").val();
	var nil = "";
    nil = $('input:radio[name=get_nota]:checked').val();
   $("#no_nota_id_"+id).val(nil);
   $("#nota_note_"+id).val($('input:radio[name=get_nota]:checked').attr('nomor_nota'));
}); 

}); */
</script>